package timeline;

import java.io.*;
import java.util.*; //import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class GetURLCopies {
	GetURLCopies() {
	}

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		if (args.length < 5) {
			System.out
					.println("Usage: getURLCopies [webPageDir] [startDate] [Duration] [outputFile] [outputFile2].");
			System.out
					.println("Example: getURLCopies /workspace/JAVA/output/webpage 20090101 1 /workspace/JAVA/output/result.txt /workspace/JAVA/output/result2.txt");
			return;
		}
		try {
			System.out.println("Webpage dir: " + args[0] + ".\nStart date: "
					+ args[1] + ",  Duration: " + args[2] + " day(s).");
			System.out.println("Output result file: " + args[3] + ".");

			String webdir = args[0];
			Calendar startCan = Calendar.getInstance();
			startCan.clear();
			startCan.set(Integer.valueOf(args[1].substring(0, 4)).intValue(),
					Integer.valueOf(args[1].substring(4, 6)).intValue() - 1,
					Integer.valueOf(args[1].substring(6, 8)).intValue());
			Integer duration = Integer.valueOf(args[2]);
			String resFile = args[3];
			String misFile = args[4];

			File res = new File(resFile);
			res.delete();
			res.createNewFile();
			BufferedWriter fw = new BufferedWriter(new FileWriter(res));
			fw.write("URL\tDay 0\tDay 1\tDay 2\t...\n");

			File misres = new File(misFile);
			misres.delete();
			misres.createNewFile();
			BufferedWriter fw2 = new BufferedWriter(new FileWriter(misres));
			fw2.write("URL\tDay 0\tDay 1\tDay 2\t...\n");

			int total1 = 0;
			int total2 = 0;

			for (int i = 1; i <= duration; i++) {
				Date startDate = startCan.getTime();

				Integer d = startDate.getDate() + 100;
				Integer m = startDate.getMonth() + 1 + 100;
				Integer y = startDate.getYear() + 1900;
				String dateStr = y.toString() + m.toString().substring(1, 3)
						+ d.toString().substring(1, 3);

				String chkDir = webdir + "/" + dateStr; // /media/disk/Study/workspace/JAVA/output/webpage/20090105
				File d1 = new File(chkDir);
				if (d1.exists() && d1.isDirectory()) {
					String[] alldirs = d1.list();
					for (int subDirIndex = 0; subDirIndex < alldirs.length; subDirIndex++) {
						int newsd = 0;
						boolean flag = false;
						boolean added = false;
						boolean misprint = false;

						String subDir = chkDir + "/" + alldirs[subDirIndex]; // 0000000000
						File d2 = new File(subDir);
						ArrayList<String> strArr = new ArrayList<String>();
						if (d2.isDirectory()) {
							String[] alldays = d2.list();
							for (int subDaysIndex = 0; subDaysIndex < alldays.length; subDaysIndex++) {
								String subDayDir = subDir + "/"
										+ alldays[subDaysIndex];// 0
								File d3 = new File(subDayDir);
								if (d3.isDirectory()) {
									String[] allTypes = d3.list();
									String logFile = subDayDir + "/"
											+ allTypes[0]
											+ "/wget_currentpage.log"; // Firefox3.0.1/wget_currentpage.log
									File d4 = new File(logFile);
									if (d4.isFile() && d4.canRead()) {
										BufferedReader fr = new BufferedReader(
												new FileReader(d4));
										String s;
										int li = 0;
										String u1 = "", u2 = "", utime = "";

										boolean found = false;
										while ((s = fr.readLine()) != null) {
											// if (li == 0 &&
											// s.matches("^--"+y.toString()+"-"
											// +m.toString().substring(1, 3)+"-"
											// +d.toString().substring(1,
											// 3)+".*$")) {
											if (li == 0 && s.matches("^--.*$")) {
												u1 = s.substring(14, s.length());
												utime = "--" + y.toString()+"-"+m.toString().substring(1, 3)+"-"
												 +d.toString().substring(1, 3)+" "+ s.substring(2, 13);
												//u1 = s.substring(25, s.length());
												//utime = s.substring(0, 24);
											} else if (s
													.matches("^Location:.*\\[following\\]$")) {
												u2 = s.substring(10,
														s.length() - 12);
												if (!misprint) {
													if (strArr.size() > 0) {
														for (int id = 0; id < strArr
																.size(); id++) {
															if (strArr.get(id)
																	.compareTo(
																			u2) != 0) {
																misprint = true;
																flag = true;
																fw2.write(u1
																		+ "\t");
																for (int tid = 0; tid < strArr
																		.size(); tid++) {
																	fw2
																			.write(strArr
																					.get(tid)
																					+ "\t");
																}
																fw2.write(u2
																		+ "\n");
																break;
															}
														}
													}
													strArr.add(u2);
												}
												found = true;
												break;
											}
											li++;
										}
										if (found && (u1.compareTo(u2) != 0)) {
											if (newsd == 0) {
												fw.write("\n");
											}
											if (Integer.valueOf(
													alldays[subDaysIndex])
													.intValue() == 0)
												fw.write(u1 + "\t");
											fw.write(u2 + "\t");

											if (newsd == 1)
												total1++;
											if (flag && (newsd > 0)) {
												if (!added)
													total2++;
												added = true;
											}
										}

										newsd++;
										fr.close();
										System.out.println(utime
												+ alldirs[subDirIndex] + "."
												+ alldays[subDaysIndex] + ":  "
												+ u1 + "  ->  " + u2);
									}
								}
							}
						}
					}
				}

				startCan.add(Calendar.HOUR, 24);
			}
			fw.write("\n");
			fw
					.write("----------------------------------------------------------------------\n");
			fw.write("Totally " + total1
					+ " instances with more than 2 days data.\n");
			fw.write("And " + total2
					+ " mismatch instances with more than 2 days data.\n");
			fw
					.write("----------------------------------------------------------------------\n");
			fw.close();
			fw2.write("\n");
			fw2.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
